﻿Imports ATS.Business.Entities
Imports ATS.UI.Process

Partial Public Class frmChangePassword
    Dim numberOfTries As Integer
    Dim passwordRetries As Integer
    Private _upc As SubmitterUserProcess = Nothing
    Dim key As String = "ATSbyJOKOforGENCPROSv2.1"


    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        DialogResult = False
        Me.Close()
    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

        If txtOldPassword.Password.Length > 0 And txtNewPassword.Password.Length > 0 And txtConfirmPassword.Password.Length > 0 Then
            If txtNewPassword.Password = txtConfirmPassword.Password Then
                Dim userDetail As New UserTable
                userDetail = _upc.GetUserDetails(Main.v_username)

                Dim tripleDES As New tripleDes
                tripleDES.secretKey = Key
                tripleDES.data = txtOldPassword.Password
                tripleDES.encryptdata()
                

                If _upc.VerifyHash(tripleDES.encryptedData, userDetail.Hashed, userDetail.Salt) Then
                    Dim tripleDESNP = New tripleDes
                    tripleDESNP.secretKey = key
                    tripleDESNP.data = txtNewPassword.Password
                    If tripleDESNP.encryptdata() Then
                        userDetail.Hashed = tripleDESNP.encryptdata()
                    Else
                        Throw New Exception("Could not encrypt password.")
                    End If

                    _upc.ChangePassword(userDetail)
                Else
                    MessageBox.Show("Old password does not match.")
                End If


            Else
                MessageBox.Show("New password does not match.")
            End If
        Else
            MessageBox.Show("All fields are required.")
        End If

    End Sub

End Class